//
//  OtherDetailHeadView.swift
//  ImGuider X
//
//  Created by llt on 2018/9/27.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit
import FSPagerView

class OtherDetailHeadView: UIView {

    private var pageView: FSPagerView!
    
    var pageControl: FSPageControl!

    
    /// 当前第几张
    var currentIndex:Int = 0

    /// 点击调用
    var selectIndex:((Int)->Void)?
    
    /// 是否自动轮播 默认true
    var autoScroll:Bool = true
    
    /// 图片填充模式 默认 scaleAspectFill
    var imageContentMode:UIView.ContentMode = .scaleAspectFill
    
    /// 设置图片数组
    var pics:Array<Any>? {
        
        didSet {
            
            pageView.reloadData()
            pageView.automaticSlidingInterval = autoScroll ? 5 : 0
            
            if let count = pics?.count, count > 1, count < 9 {
                
                pageControl.isHidden = false
                pageControl.numberOfPages = count
            } else {
                
                pageControl.isHidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)

        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        pageView = FSPagerView(frame: bounds)
        pageView.dataSource = self
        pageView.delegate = self
        pageView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "FSPagerViewCell")

        self.addSubview(pageView)
        
        pageControl = FSPageControl()
        pageControl.contentHorizontalAlignment = .center
        
        self.addSubview(pageControl)
        
        pageControl.snp.makeConstraints {
            
            [weak self] maker in
            guard let `self` = self else { return }
            
            maker.left.equalToSuperview()
            maker.right.equalToSuperview()
            maker.bottom.equalTo(self.snp.bottom).offset(-50)
            maker.height.equalTo(30)
            
        }
    }

    
    /// 设置下拉放大
    ///
    /// - Parameter offset: 下拉y偏移量
    func setFactor(offset:CGFloat) {
        
        if offset >= 0 {
            
            pageView.automaticSlidingInterval = self.autoScroll ? 5 : 0
            return
        }
        
        self.autoScroll = false
        
        pageView.automaticSlidingInterval = 0
        
        let height = frame.size.height - offset
//        let width = frame.size.width / frame.size.height * height
//        let x = (frame.width - width) / 2
        
        self.pageView.frame = CGRect(x: 0, y: offset, width: frame.width, height: height)
    }
}

extension OtherDetailHeadView: FSPagerViewDataSource, FSPagerViewDelegate {
    
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        
        return pics?.count ?? 0
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "FSPagerViewCell", at: index)
        cell.imageView?.contentMode = self.imageContentMode
        cell.clipsToBounds = true
        
        cell.contentView.layer.shadowOpacity = 0
        
        if let pic = pics?[index] as? UIImage {
            
            cell.imageView?.image = pic
        } else if let pic = pics?[index] as? String {
            
            cell.imageView?.setImage(string: pic, placeholder: UIImage(named: "placeholder_rec")!)
        }

        return cell
    }
    
    
    
    func pagerViewWillEndDragging(_ pagerView: FSPagerView, targetIndex: Int) {
        
        pageControl.currentPage = targetIndex
    }
    
    func pagerViewDidEndScrollAnimation(_ pagerView: FSPagerView) {
        
        pageControl.currentPage = pagerView.currentIndex
        currentIndex = pagerView.currentIndex
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        
        pagerView.deselectItem(at: index, animated: true)
        
        selectIndex?(index)
    }
}
